$(function(){
    bindingLoginAction(); 
//	$("#div14").show();
});
function bindingLoginAction(){
    $("#loginBtn").click(loginFromHead);
    $("#logoutBtn").click(logout);
    $("#showRegister").click(showRegister);
}



function showLogin(){
	//alert("dkf");
	$("#div14").show();
	$("#login").css("z-index","16");
	$("#register").css("z-index","15");
	$("#login").show();
	$("#register").show();
}

function showRegister(){
	$("#div14").show();
	$("#login").css("z-index","15");
	$("#register").css("z-index","16");
	$("#login").show();
	$("#register").show();
}









//=================================================================



function doRegister(){
    var username = $("#floatbox-box #regUsername").val();
    var password = $("#floatbox-box #regPassword").val();
    var rePassword=$("#floatbox-box #regRePassword").val();
    var email = $("#floatbox-box #regEmail").val();
    var verifyCode = $("#floatbox-box #regCode").val();
    //check the code 
    //if(checkCode(theCode)){
    $.ajax({
        async:false,
        method:"get",
        url:"do?module=user",
        dataType:"json",
        data:{
            "action":"register",
            "username":username,
            "password":password,
            "email":email,
            "verify":verifyCode
        },
        success:function(returnData){
            if(returnData.status == "success"){
                removeFloatbox();
            }
        }
    });
//end after checkCode
//}else{
//show error dialog;
//}
}


/*
function floatbox(loginOrRegister){
    removeFloatbox();
    $.floatbox({
        bgConfig:{
            position: "fixed",
            zIndex: 8,
            backgroundColor: "black",
            opacity: 0.75,
            top:0,
            left:0
        },
        boxConfig:{
            position: "fixed",
            zIndex: 9,
            width: "auto",            
            height: "auto",
            top: "50%",
            left: "50%",
            backgroundColor: "#fff",
            //            marginTop: "-160",//negative half of height value;
            marginLeft: "-126px" //negative half of width value;
        },
        content: $("#"+loginOrRegister+"Form").html()
    });
}*/
function loginFromHead(){
    var username = $("#username").val();
    var password = $("#password").val();
//    console.log(username);
    login(username, password);
}
function loginFromForm(){
    var username = $("#floatbox-box #formUsername").val();
    var password = $("#floatbox-box #formPassword").val();
   
    login(username, password);
}
function changeVerify(){
    var imageDom = $(".verifyCode");
    $(imageDom).attr("src","do?module=verify&random="+Math.random());
}
function login(username , password){
    //check form fields values
    //    $("").validate({
    //        rules:{
    //            
    //        }
    //    });
    //ajax checklogin
    $.ajax({
        async:false,
        method:"get",
        dataType:"json",
        url:"do?module=user",
        data:{
            "action":"login",
            "username":username,
            "password":password
        },
        success:function(returnData){
            if(returnData.status == "success"){
                $(".logined").removeClass("hidden");
                $(".nologin").addClass("hidden");
                $("#loginform").addClass("hidden");
                $("#loginuser").html(returnData.username);
                removeFloatbox();
            }else{
                alert("errror!")
            }            
        }
    });
}

function removeFloatbox(){
    $("#floatbox-box").remove();
    $("#floatbox-background").remove();
}

function logout(){
    //ajax remove login session   
    location.href = "do?module=user&action=logout";
//    $(".nologin").removeClass("hidden");
//    $(".logined").addClass("hidden");
//    $("#loginuser").html("");
}
